package com.sky.loveshop.controller;

import cn.hutool.json.JSONUtil;
import com.sky.loveshop.entity.ReceiveAddress;
import com.sky.loveshop.entity.Result;
import com.sky.loveshop.entity.exception.BizException;
import com.sky.loveshop.entity.exception.BizExceptionEnum;
import com.sky.loveshop.entity.qo.ReceiveAddressQO;
import com.sky.loveshop.service.ReceiveAddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xcy
 * @version V1.0
 * @description 收获地址管理
 * @date 2024/12/28 2:13
 */
@Slf4j
@RestController
@RequestMapping("/receiveAddress")
public class ReceiveAddressController {

    @Autowired
    private ReceiveAddressService receiveAddressService;

    /**
     * 添加收货地址
     */
    @PostMapping("/addReceiveAddress")
    public Result<Integer> addReceiveAddress(@RequestBody ReceiveAddressQO qo) {
        log.info("addReceiveAddressParam={}", JSONUtil.toJsonStr(qo));
        receiveAddressService.addReceiveAddress(qo);
        return Result.success();
    }

    /**
     * 通过id删除收货地址
     */
    @GetMapping("/delReceiveAddressById")
    public Result<Integer> delReceiveAddressById(Integer id) {
        log.info("delReceiveAddressByIdParam={}", id);
        receiveAddressService.delReceiveAddressById(id);
        return Result.success();
    }

    /**
     * 查询我的收获地址
     */
    @GetMapping("/getMyReceiveAddressList")
    public Result<List<ReceiveAddress>> getMyReceiveAddressList(Integer userId) throws BizException {
        if (userId == null) {
            throw new BizException(BizExceptionEnum.USER_ID_MUST_NOT_NULL);
        }
        return Result.success(receiveAddressService.getMyReceiveAddressList(userId));
    }

}
